import React, {Component} from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import cssModules from 'react-css-modules';
import classnames from 'classnames';
import {Form, Row, Button, Radio} from 'antd';
import {FormInput, FormItemCustom} from '../../component/FormItem';
import {addUnitInfo} from 'util/api';
import styles from './style.less';

@inject('IdRecordDetailStore')
@observer
@cssModules(styles)
class AddUnitContent extends Component {
  constructor(props) {
    super(props);
    this.store = this.props.IdRecordDetailStore;
  }

  render() {
    const {fields, handleFormChange} = this.store;
    let scale = Math.ceil((130 / 600) * 24);
    const formItemLayout = {
      labelCol: {
        sm: {span: scale},
      },
      wrapperCol: {
        sm: {span: 24 - scale},
      },
    };
    const CustomRadioComponent = props => {
      const {children = []} = props;

      return (
        <Row className={styles['custom-radio-wrap']}>
          {children.map((item, index) => {
            let isSelected = item.value === props[props.name].value;

            return (
              <Button
                key={index.toString(36)}
                disabled={props.disabled}
                className={classnames(
                  styles['radio-button'],
                  isSelected && styles['radio-selected'],
                )}
                onClick={() => {
                  let obj = {};
                  props[props.name]['value'] = item.value;
                  obj[props.name] = props[props.name];
                  handleFormChange(obj);
                }}>
                {item.name}
              </Button>
            );
          })}
        </Row>
      );
    };

    return (
      <Form
        {...formItemLayout}
        className={styles['add-users-wrap']}
        autoComplete="off">
        <FormInput
          {...fields}
          onChange={handleFormChange}
          placeholder={'请输入姓名'}
          label={'姓名'}
          disabled={true}
          name={'cname'}
          rules={[
            {
              required: false,
              message: '必须填写内容!',
            },
          ]}
        />
        <FormInput
          {...fields}
          onChange={handleFormChange}
          label={'识别时间'}
          disabled={true}
          name={'recordTime'}
          rules={[
            {
              required: false,
              message: '必须填写内容!',
            },
          ]}
        />
        <FormInput
          {...fields}
          onChange={handleFormChange}
          label={'识别位置'}
          disabled={true}
          name={'recordLocation'}
          rules={[
            {
              required: false,
              message: '必须填写内容!',
            },
          ]}
        />

        <FormItemCustom
          {...fields}
          onChange={handleFormChange}
          placeholder={'请输入户籍'}
          label={'用户类型'}
          disabled={true}
          name={'userType'}
          rules={[
            {
              required: true,
              message: '必须填写内容!',
            },
          ]}
          children={[
            {value: 0, name: '陌生人'},
            {value: 1, name: '业主'},
            {value: 2, name: '租户'},
            {value: 3, name: '家属'},
            {value: 4, name: '亲友'},
            {value: 5, name: '保洁员'},
            {value: 6, name: '物管员'},
            {value: 7, name: '维修员'},
            {value: 8, name: '保安员'},
          ]}
          RenderComponet={CustomRadioComponent}
        />
      </Form>
    );
  }
}

export default AddUnitContent;
